const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');//验证信息

const app = express();

// 引入users.js profile.js post.js
const users = require("./routers/api/users");
const profile = require("./routers/api/profile");
const post = require("./routers/api/posts")
// body-parser node获取数据的中间件
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// 数据库地址
const db = require("./config/keys").mongoURL;
// 链接数据库
mongoose.connect(db)
        .then(()=>{
            console.log("数据库链接成功");
        })
        .catch(err =>{
            console.log(err);
        })
// 使用中间件实现允许跨域
// app.use((req,res,next) =>{
//     res.header("Access-Control-Allow-Origin","*");
//     res.header("Access-Control-Allow-Headers","Content-Type");
//     res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELECT,OPTIONS");
//     next();
// })
// 初始化passport
app.use(passport.initialize());
require("./config/passport")(passport);        
// 设置路由
// 使用routers
app.use('/api/users',users);
app.use('/api/profile',profile);
app.use('/api/posts',post)
const port = process.env.port ||  5000;
app.listen(port,()=>{
    console.log(`server is running port ${port}`)
})